package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.ProductDO;
import com.bootdo.system.domain.ProjectDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.ProjectService;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.UserInnerDO;
import com.bootdo.system.service.UserInnerService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目成员
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-06-25 22:39:17
 */
 
@Controller
@RequestMapping("/system/userInner")
public class UserInnerController {
	@Autowired
	private UserInnerService userInnerService;
	@Autowired
	private UserService userService;
	@Autowired
	private ProjectService projectService;
	
	@GetMapping()
	@RequiresPermissions("system:userInner:userInner")
	String UserInner(HttpServletRequest request){

		String username = request.getSession().getAttribute("username").toString();

		UserDO userDO = userService.getUserByUsername(username);

		List<ProjectDO> projects = projectService.getProjectByUserId(userDO.getUserId());


		request.setAttribute("projects",projects);
		if(projects.size()>0){
			request.setAttribute("projectDo",projects.get(0));
		}else{
			ProjectDO projectDO = new ProjectDO();
			projectDO.setProjectId(-1);
			projectDO.setProjectName("没有找到项目");
			request.setAttribute("projectDo",projectDO);
		}



	    return "system/userInner/userInner";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:userInner:userInner")
	public PageUtils list(@RequestParam Map<String, Object> params,HttpServletRequest request){
        String username = request.getSession().getAttribute("username").toString();

        UserDO userDO = userService.getUserByUsername(username);
        params.put("userId",userDO.getUserId());
		System.out.println("userid="+userDO.getUserId());
        //查询列表数据
		System.out.println("项目成员param="+params);
        Query query = new Query(params);
		List<UserInnerDO> userInnerList = userInnerService.list(query);
		int total = userInnerService.count(query);

		params.put("limit",Integer.MAX_VALUE);
		params.put("offset",0);
		Object projectIdforchoose = params.get("projectIdforchoose");
		if(projectIdforchoose.toString().equals("-1")){
			params.remove("projectIdforchoose");
			List<UserInnerDO> fortotal = userInnerService.list(params);
			UserInnerDO userInnerDO = fortotal.get(0);
			System.out.println("默认项目id="+userInnerDO.getProjectId());
			if(userInnerDO!=null){
				params.put("projectIdforchoose",userInnerDO.getProjectId());
			}

		}

		List<UserInnerDO> fortotal = userInnerService.list(params);

		PageUtils pageUtils = new PageUtils(userInnerList,fortotal.size());
		System.out.println("成员total="+total);
		return pageUtils;
	}
	
	@GetMapping("/add/{projectId}")
	@RequiresPermissions("system:userInner:add")
	String add(Model model, HttpServletRequest request,@PathVariable("projectId") Integer projectId){

        HashMap<String, Object> map = new HashMap<>();
        map.put("deptId","");

        List<UserDO> users = userService.list(map);
        model.addAttribute("users",users);


        model.addAttribute("projectId",projectId);
        model.addAttribute("projectDo",projectService.get(projectId));

        HashMap<String, Object> params = new HashMap<>();
        params.put("limit",Integer.MAX_VALUE);
        params.put("offset","0");

        String username = request.getSession().getAttribute("username").toString();
        UserDO userDo = userService.getUserByUsername(username);
        System.out.println("userdo="+userDo.getUserId());
        params.put("userId",userDo.getUserId());
        //查询列表数据
        Query query = new Query(params);
        List<ProjectDO> projects = projectService.list(query);
        System.out.println(projects);
        model.addAttribute("projects",projects);

        return "system/userInner/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:userInner:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		UserInnerDO userInner = userInnerService.get(id);
		model.addAttribute("userInner", userInner);
	    return "system/userInner/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:userInner:add")
	public R save( UserInnerDO userInner){
		if(userInner.getUserId()==null){
			return R.error(444,"请选择用户");
		}
		if(userInner.getProjectId()==null){
			return R.error(444,"请选择项目再试");
		}
		UserInnerDO userInnerDO = userInnerService.getUserInnerByUserIdAndProjectId(userInner.getUserId(),userInner.getProjectId());

		if(userInnerDO!=null){
			return R.error(444,"该项目已经存在这个用户");
		}

		if(userInnerService.save(userInner)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:userInner:edit")
	public R update( UserInnerDO userInner){
		userInnerService.update(userInner);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:userInner:remove")
	public R remove(Integer id){
		if(userInnerService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:userInner:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		userInnerService.batchRemove(ids);
		return R.ok();
	}
	
}
